@echo off
REM Visual Studio 2022 部署脚本

setlocal enabledelayedexpansion

echo RemoteMaster Visual Studio 2022 部署脚本
echo ========================================

REM 设置变量
set SOLUTION_DIR=%~dp0..\..
set BUILD_DIR=%SOLUTION_DIR%\build_vs2022
set DEPLOY_DIR=%SOLUTION_DIR%\deploy\windows\package
set QT_DIR=C:\Qt\6.2\msvc2019_64
set WINDEPLOYQT=%QT_DIR%\bin\windeployqt.exe
set CONFIG=Release

echo.
echo 配置信息:
echo 解决方案目录: %SOLUTION_DIR%
echo 构建目录: %BUILD_DIR%
echo 部署目录: %DEPLOY_DIR%
echo Qt目录: %QT_DIR%
echo 配置: %CONFIG%
echo.

REM 检查构建目录
if not exist "%BUILD_DIR%" (
    echo 错误: 未找到构建目录，请先使用configure_vs2022.bat配置项目
    pause
    exit /b 1
)

REM 检查可执行文件
if not exist "%BUILD_DIR%\%CONFIG%\RemoteMaster.exe" (
    echo 错误: 未找到RemoteMaster.exe，请先在Visual Studio中构建项目
    pause
    exit /b 1
)

REM 检查Qt部署工具
if not exist "%WINDEPLOYQT%" (
    echo 警告: 未找到windeployqt工具，请检查Qt安装路径
    echo 当前Qt路径: %QT_DIR%
    pause
    exit /b 1
)

REM 创建部署目录
if exist "%DEPLOY_DIR%" (
    echo 删除现有部署目录...
    rmdir /s /q "%DEPLOY_DIR%"
)

echo 创建部署目录...
mkdir "%DEPLOY_DIR%"

REM 复制可执行文件
echo 复制可执行文件...
copy "%BUILD_DIR%\%CONFIG%\RemoteMaster.exe" "%DEPLOY_DIR%\"

REM 复制依赖库
echo 复制Qt依赖库...
"%WINDEPLOYQT%" --dir "%DEPLOY_DIR%" "%DEPLOY_DIR%\RemoteMaster.exe"

REM 复制其他必要文件
echo 复制附加文件...
copy "%SOLUTION_DIR%\README.md" "%DEPLOY_DIR%\"
if exist "%SOLUTION_DIR%\LICENSE" (
    copy "%SOLUTION_DIR%\LICENSE" "%DEPLOY_DIR%\"
)

REM 复制资源文件
echo 复制资源文件...
mkdir "%DEPLOY_DIR%\resources"
if exist "%SOLUTION_DIR%\resources\icons" (
    xcopy "%SOLUTION_DIR%\resources\icons" "%DEPLOY_DIR%\resources\icons\" /E /I /Y
)
if exist "%SOLUTION_DIR%\resources\style.qss" (
    copy "%SOLUTION_DIR%\resources\style.qss" "%DEPLOY_DIR%\resources\"
)

echo.
echo 部署完成!
echo 部署文件位于: %DEPLOY_DIR%
echo.

REM 询问是否创建安装程序
echo 是否要创建安装程序? (需要安装NSIS)
choice /C YN /M "创建安装程序"
if !ERRORLEVEL! EQU 1 (
    echo 创建安装程序...
    set NSIS_DIR=C:\Program Files (x86)\NSIS
    if exist "%NSIS_DIR%\makensis.exe" (
        "%NSIS_DIR%\makensis.exe" "%SOLUTION_DIR%\deploy\windows\installer.nsi"
        if !ERRORLEVEL! EQU 0 (
            echo 安装程序创建成功!
        ) else (
            echo 安装程序创建失败!
        )
    ) else (
        echo 未找到NSIS，请安装NSIS以创建安装程序
    )
)

echo.
echo 所有操作完成!
pause